// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-04-24 10:28:09
// 生成路径: internal/app/business/logic/venues.go
// 生成人：周志雄
// desc:演出场馆
// company:云南奇讯科技有限公司
// ==========================================================================

package logic

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/tiger1103/gfast/v3/api/v1/business"
	"github.com/tiger1103/gfast/v3/internal/app/business/dao"
	"github.com/tiger1103/gfast/v3/internal/app/business/model"
	"github.com/tiger1103/gfast/v3/internal/app/business/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/business/service"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterVenues(New())
}

func New() *sVenues {
	return &sVenues{}
}

type sVenues struct{}

// 根据场馆ID查询座位列表
func (s *sVenues) GetSeatsById(ctx context.Context, req *business.GetSeatsByVenueIdReq) (res *business.GetSeatsByVenueIdRes, err error) {
	res = new(business.GetSeatsByVenueIdRes)
	err = g.Try(ctx, func(ctx context.Context) {
		// 查询所有座位信息，根据场馆ID过滤
		err = dao.Seats.Ctx(ctx).WithAll().Where(dao.Seats.Columns().VenueId, req.VenueId).Scan(&res.Seats)
		liberr.ErrIsNil(ctx, err, "获取座位失败")
	})
	if err != nil {
		return nil, err
	}

	return res, nil
}

func (s *sVenues) List(ctx context.Context, req *business.VenuesSearchReq) (listRes *business.VenuesSearchRes, err error) {
	listRes = new(business.VenuesSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.Venues.Ctx(ctx).WithAll()

		if req.VenueName != "" {
			m = m.Where(dao.Venues.Columns().VenueName+" like ?", "%"+req.VenueName+"%")
		}

		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"

		var res []*model.VenuesInfoRes
		err = m.Fields(business.VenuesSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.VenuesListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.VenuesListRes{
				Id:        v.Id,
				VenueName: v.VenueName,
				CreatedAt: v.CreatedAt,
			}
		}
	})
	return
}

func (s *sVenues) GetById(ctx context.Context, id int) (res *model.VenuesInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.Venues.Ctx(ctx).WithAll().Where(dao.Venues.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sVenues) Add(ctx context.Context, req *business.VenuesAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Venues.Ctx(ctx).Insert(do.Venues{
			VenueName: req.VenueName,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sVenues) Edit(ctx context.Context, req *business.VenuesEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Venues.Ctx(ctx).WherePri(req.Id).Update(do.Venues{
			VenueName: req.VenueName,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sVenues) Delete(ctx context.Context, ids []int) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Venues.Ctx(ctx).Delete(dao.Venues.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}
